package pfsimulator;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

/**
 * PNode represent a Physical Node in the cluster
 */
public class PNode {
    private long dpip;
    private List<VNode> instances;
    private long aggregateBw;
    private static Random dice = new Random(System.currentTimeMillis());

    public PNode(long dpip, long aggregateBw) {
        this.dpip = dpip;
        this.instances = new LinkedList<VNode>();
        this.aggregateBw = aggregateBw;
    }

    //
    // APIs
    //

    /**
     * Called when constructing the graph to
     * add a VM (VNode) to this physical
     * host (PNode)
     * */
    public void addVNode(VNode vNode){
        vNode.setPhyHost(this);
        instances.add(vNode);
    }

    /**
     * Get VNode given its host name
     * */
    public VNode getVNode(String vnodeName){
        for (int i = 0; i < instances.size(); i++) {
            VNode vNode =  instances.get(i);
            if(vNode.getHostName().equals(vnodeName)){
                return vNode;
            }
        }
        return null;
    }

    // Getters & Setters

    public long getAggregateBw() {
        return aggregateBw;
    }

    public void setAggregateBw(long aggregateBw) {
        this.aggregateBw = aggregateBw;
    }

    public long getDpip() {
        return dpip;
    }

    public void setDpip(long dpip) {
        this.dpip = dpip;
    }

    // Return a copy of VNode list
    public List<VNode> getAllVNodes() {
        return new LinkedList<VNode>(this.instances);
    }

    /**
     * Return a random VNode on this PNode
     * Return null if no VNode is on this PNode
     * */
    public VNode getRandomVNode() {
        return this.instances.get(dice.nextInt(instances.size()));
    }
}
